package domain;

import java_persistence.OrdersMapper;

/**
 * @author joseph simoenau <simoneau@gatech.edu>
 *
 */
public class Orders {
	
	private String prescription;
	private String labWork;
	private String follup_instructions;
	private String other_instructions;

	public Orders() {
		super();
	}
	
	public Orders( String p, String lw, String fi, String oi) {
		super();
		this.prescription = p;
		this.labWork = lw;
		this.follup_instructions = fi;
		this.other_instructions = oi;
	}

	public String getPrescription() {
		return prescription;
	}
	public String getLabWork() {
		return this.labWork;
	}
	public String getFollup_instructions() {
		return follup_instructions;
	}
	public String getOther_instructions() {
		return other_instructions;
	}

	public void setPrescription(String prescription) {
		this.prescription = prescription;
	}
	public void setLabWork(String labWork) {
		this.labWork = labWork;
	}
	public void setFollup_instructions(String follup_instructions) {
		this.follup_instructions = follup_instructions;
	}
	public void setOther_instructions(String other_instructions) {
		this.other_instructions = other_instructions;
	}
	
	
	// Database Functionality
	
	
	// CREATES
	
	/**Stores 'this' to the database attached to the given TreatmentRecord and Appointment.
	 * @param tr the associated TreatmentRecord
	 * @param a the associated Appointment
	 * @param ordmap retrieved from a valid, open SqlSession object.
	 */
	public void create_orders(TreatmentRecord tr, Appointment a, OrdersMapper ordmap) {
		if( a.getDate()!=null && a.getPatient().getUsername()!=null && tr.getAttending_doctor()!=null ){
			// TR & Appointment should have been saved.
			ordmap.create_orders(a.getDate(), a.getPatient().getUsername(),this.prescription, this.labWork, this.follup_instructions, this.other_instructions);
		}
	}
	
	// RETRIEVES
	
	/**Retrieves the orders associated with a given treatment record and appointment from the database and populates 'this' with its data.
	 * @param tr the associated TreatmentRecord
	 * @param a the associated Appointment 
	 * @param ordmap retrieved from a valid, open SqlSession object.
	 */
	public void retrieve(TreatmentRecord tr, Appointment a, OrdersMapper ordmap) {
		Orders tmp = ordmap.retrieve(a.getDate(), a.getPatient().getUsername());
		
		if ( tmp != null ) {
			this.follup_instructions = tmp.follup_instructions;
			this.other_instructions = tmp.other_instructions;
			this.prescription = tmp.prescription;
			this.labWork = ordmap.get_lab_work(a.getDate(), a.getPatient().getUsername());
		}
		else {
			this.follup_instructions = null;
			this.labWork = null;
			this.other_instructions = null;
			this.prescription = null;
		}
		
	}
	
	// UPDATES
	
	/**Updates 'this''s entry in the database to its current values.   
	 * @param tr the associated TreatmentRecord
	 * @param a the associated Appointment
	 * @param ordmap retrieved from a valid, open SqlSession object.
	 */
	public void update_orders(TreatmentRecord tr, Appointment a, OrdersMapper ordmap) {
		if( a.getDate()!=null && a.getPatient().getUsername()!=null && tr.getAttending_doctor()!=null ){
			// TR & Appointment should have been saved.
			ordmap.update_orders(a.getDate(), a.getPatient().getUsername(),this.prescription, this.labWork, this.follup_instructions, this.other_instructions);
		}
	}
	
	// DELETES
	
	/**Deletes 'this''s entry from the database.  Please note that this method shoul not be used to delete records.  It exists primarily for testing purproses.  
	 * @param tr the associated TreatmentRecord - depricated.  may as well be null
	 * @param a the associated Appointment
	 * @param ordmap retrieved from a valid, open SqlSession object. 
	 */
	public void delete_orders(TreatmentRecord tr, Appointment a, OrdersMapper ordmap) {
		ordmap.delete_orders(a.getDate(), a.getPatient().getUsername());
	}
	
}
